package com.lizhangjun.nim.configuration;

import com.lizhangjun.nim.constant.ImConstant;
import com.lizhangjun.nim.listener.RedisMQMsgListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * @author lizhangjun
 */
@Component
public class RedisListenConfig {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Bean
    @ConditionalOnMissingBean(RedisMessageListenerContainer.class)
    public RedisMessageListenerContainer container() {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();

        listenerContainer.setConnectionFactory(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
        //监听IM推送消息
        listenerContainer.addMessageListener(new RedisMQMsgListener(), new ChannelTopic(ImConstant.TOPIC_IM));
        return listenerContainer;
    }
}

